{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ca477bd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    fjac: array([[-1.]])\n",
       "     fun: array([0.])\n",
       " message: 'The solution converged.'\n",
       "    nfev: 3\n",
       "     qtf: array([0.])\n",
       "       r: array([1.])\n",
       "  status: 1\n",
       " success: True\n",
       "       x: array([1.])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.optimize import root,fsolve\n",
    "def f1(x):\n",
    "    return x**2-3*x+2\n",
    "root(f1,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75ab88e1",
   "metadata": {},
   "source": [
    "Python 求解非线性方程\n",
    "如下方程，求解x1,x2,x3:\n",
    "5x1 + 3 = 0\n",
    "4x0x0 - 2sin(x1x2) = 0\n",
    "x1x2 - 1.5 = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "353816b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.70622057 -0.6        -2.5       ]\n"
     ]
    }
   ],
   "source": [
    "from scipy.optimize import fsolve\n",
    "from math import sin,cos\n",
    "\n",
    "def f(x):\n",
    "    x0 = float(x[0])\n",
    "    x1 = float(x[1])\n",
    "    x2 = float(x[2])\n",
    "    return [\n",
    "        5*x1+3,\n",
    "        4*x0*x0 - 2*sin(x1*x2),\n",
    "        x1*x2 - 1.5\n",
    "    ]\n",
    "\n",
    "result = fsolve(f, [1,1,1])\n",
    "\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e9c61387",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5000000000000053"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[2]*result[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a22c09b2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
